package gui;

import interfaces.Command;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import mediator.Mediator;

public class LoginDialog extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	
	Mediator med;
	public JTextField usernameTxt;
	private JPasswordField passwordTxt;

	public static Logger logger = Logger.getLogger(LoginDialog.class);
	
	/**
	 * Create the dialog.
	 */
	public LoginDialog(Mediator m) {
		setTitle("Login to Auction House");
		med = m;
		m.registerDialog(this);
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblUsername.setBounds(46, 57, 71, 23);
		contentPanel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPassword.setBounds(46, 115, 71, 23);
		contentPanel.add(lblPassword);
		
		setUsernameTxt(new JTextField("mihai"));
		getUsernameTxt().setBounds(127, 42, 260, 36);
		contentPanel.add(getUsernameTxt());
		getUsernameTxt().setColumns(10);
		
		setPasswordTxt(new JPasswordField("1234"));
		getPasswordTxt().setBounds(127, 100, 260, 36);
		contentPanel.add(getPasswordTxt());
		
		final JButton btnSimulate = new JButton("Simulate");
		btnSimulate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logger.info("!!! Simulation has been initiated !!!");
				med.simulator.execute();
				btnSimulate.setEnabled(false);
			}
		});
		btnSimulate.setToolTipText("Press this Button to Start Simulating the Application. After pressing, please wait and watch.");
		btnSimulate.setFont(new Font("Tahoma", Font.ITALIC, 11));
		btnSimulate.setForeground(SystemColor.desktop);
		btnSimulate.setBackground(SystemColor.activeCaption);
		btnSimulate.setBounds(10, 172, 89, 23);
		contentPanel.add(btnSimulate);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				LoginBtn login = new LoginBtn(this, m);
				buttonPane.add(login);
				getRootPane().setDefaultButton(login);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						logger.info("User has declined logging in. Application terminated.");
						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		((Command)e.getSource()).execute();
	}

	public JTextField getUsernameTxt() {
		return usernameTxt;
	}

	public void setUsernameTxt(JTextField usernameTxt) {
		this.usernameTxt = usernameTxt;
	}

	public JPasswordField getPasswordTxt() {
		return passwordTxt;
	}

	public void setPasswordTxt(JPasswordField passwordTxt) {
		this.passwordTxt = passwordTxt;
	}
}
